﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace TimKen.Web.Admin.Inbox
{
    public partial class Inbox_List : TimKen.App.AdminPage
    {
        TimKen.BussLogic.InboxManager inboxM = new TimKen.BussLogic.InboxManager();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindResources();
                BindData();
            }
        }

        public override string ActionCode
        {
            get
            {
                return "95";
            }
        }

        protected void BindData()
        {
            int isRead = -1;
            if (chkIsRead.Checked)
                isRead = 0;
            DataSet ds = inboxM.GetList(AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, CurrentUserName, "", isRead);
            AspNetPager1.RecordCount = Convert.ToInt32(ds.Tables[1].Rows[0]["total"]);

            this.grdInbox.DataSource = ds.Tables[0].DefaultView;
            this.grdInbox.DataBind();
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        protected void grdInbox_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "DeleteMessage")
            {
                int msgID = int.Parse(e.CommandArgument.ToString());
                TimKen.DataEntity.Inbox inbox = inboxM.GetModel(msgID);
                if (inbox.FromDelete)
                {
                    inboxM.Delete(msgID);
                }
                else
                {
                    inbox.ToDelete = true;
                    inboxM.Update(inbox);
                }
                BindData();
            }
        }

        public string GetMessageStatusPic(object IsRead)
        {
            if (bool.Parse(IsRead.ToString()))
            {
                return "<img src='../../Images/Read.gif' alt='" + GetLangString("MessageIsRead") + "' />";
            }
            else
            {
                return "<img src='../../Images/UnRead.gif' alt='" + GetLangString("MessageUnRead") + "' />";
            }
        }

        public void BindResources()
        {
            chkIsRead.Text = GetLangString("MessageUnRead");
        }

        protected void chkIsRead_CheckedChanged(object sender, EventArgs e)
        {
            BindData();
        }

    }
}
